package com.geniusk.system.controller;

import cn.hutool.core.util.IdUtil;
import cn.hutool.json.JSONUtil;
import com.geniusk.common.core.JSONResult;
import com.geniusk.security.annotation.PreFuncAuth;
import com.geniusk.security.utils.SecurityUtil;
import com.geniusk.system.model.bo.model.SelectModelByIdBO;
import com.geniusk.system.model.bo.modelEvent.DeleteModelEventByIdsBO;
import com.geniusk.system.model.bo.modelEvent.InsertModelEventBO;
import com.geniusk.system.model.bo.modelEvent.SelectModelEventByIdBO;
import com.geniusk.system.model.bo.modelEvent.UpdateModelEventBO;
import com.geniusk.system.model.pojo.ModelEvent;
import com.geniusk.system.model.pojo.ModelProperty;
import com.geniusk.system.model.pojo.Role;
import com.geniusk.system.service.ModelEventService;
import com.geniusk.system.service.PropertyEventService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 模型事件管理接口
 */
@RestController
@RequestMapping("/system/modelEvent")
public class ModelEventController {

    @Autowired
    private ModelEventService modelEventService;

    @Autowired
    private PropertyEventService propertyEventService;

    /**
     * ID查询模型事件列表
     */
    @PostMapping("/selectModelEventByModelId")
    @PreFuncAuth("SYS:MODEL:SELECT")
    public JSONResult selectModelEventByModelId(@Valid @RequestBody SelectModelByIdBO selectModelByIdBO) {
        List<ModelEvent> modelEventList = modelEventService.selectModelEventByModelId(selectModelByIdBO.getModelId());
        Map<String, Object> result = new HashMap<>();
        result.put("list",modelEventList);
        return JSONResult.ok(result);
    }


    /**
     * ID查询模型事件
     */
    @PostMapping("/selectModelEventById")
    @PreFuncAuth("SYS:MODEL:SELECT")
    public JSONResult selectModelEventById(@Valid @RequestBody SelectModelEventByIdBO selectModelEventByIdBO) {
        ModelEvent modelEvent = modelEventService.selectModelEventById(selectModelEventByIdBO.getModelEventId());
        List<ModelProperty> modelPropertyList = modelEvent.getProperties();
        List<String> propertyIds = new ArrayList<>();
        if(modelPropertyList != null){
            for(ModelProperty modelProperty : modelPropertyList){
                propertyIds.add(modelProperty.getModelPropertyId().toString());
            }
        }
        modelEvent.setPropertyIds(propertyIds);
        Map<String, Object> result = new HashMap<>();
        result.put("modelEvent",modelEvent);
        return JSONResult.ok(result);
    }

    /**
     * 添加模型事件
     */
    @PostMapping("/insertModelEvent")
    @PreFuncAuth("SYS:MODEL:INSERT")
    public JSONResult insertModelEvent(@Valid @RequestBody InsertModelEventBO insertModelEventBO) {
        ModelEvent modelEvent = JSONUtil.parse(insertModelEventBO).toBean(ModelEvent.class);
        Long modelEventId = IdUtil.getSnowflake().nextId();
        modelEvent.setModelEventId(modelEventId);
        modelEvent.setCreateBy(SecurityUtil.getUsername());
        int rows = modelEventService.insertModelEvent(modelEvent);
        propertyEventService.insertPropertyEvent(modelEventId, insertModelEventBO.getPropertyIds());
        Map<String,Object> result = new HashMap<>();
        result.put("rows",rows);
        return JSONResult.ok(result);
    }

    /**
     * 修改模型事件
     */
    @PostMapping("/updateModelEvent")
    @PreFuncAuth("SYS:MODEL:UPDATE")
    public JSONResult updateModelEvent(@Valid @RequestBody UpdateModelEventBO updateModelEventBO) {
        ModelEvent modelEvent = JSONUtil.parse(updateModelEventBO).toBean(ModelEvent.class);
        modelEvent.setUpdateBy(SecurityUtil.getUsername());
        int rows = modelEventService.updateModelEvent(modelEvent);
        Long modelEventId = modelEvent.getModelEventId();
        propertyEventService.insertPropertyEvent(modelEventId, updateModelEventBO.getPropertyIds());
        Map<String,Object> result = new HashMap<>();
        result.put("rows",rows);
        return JSONResult.ok(result);
    }

    /**
     * 删除模型事件
     */
    @PostMapping("/deleteModelEventByIds")
    @PreFuncAuth("SYS:MODEL:DELETE")
    public JSONResult deleteModelEventByIds(@Valid @RequestBody DeleteModelEventByIdsBO deleteModelEventByIdsBO) {
        int rows = modelEventService.deleteModelEventByIds(deleteModelEventByIdsBO.getModelEventIds());
        Map<String,Object> result = new HashMap<>();
        result.put("rows",rows);
        return JSONResult.ok(result);
    }
}
